package models;

import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.SqlQuery;
import com.avaje.ebean.SqlRow;
import com.avaje.ebean.SqlUpdate;

import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;


/**
 * User entity managed by Ebean
 */
@Entity
@Table(name="tests")
public class Tests extends Model {

	public static final long serialVersionUID = 1L;

	@Id
	@Column(name="idtests")
	public long id;

	@OneToOne
    @JoinColumn(name="examination_id")
    public Examination examination;

	@OneToOne
	@JoinColumn(name="appointment_id")
    public AppointmentHistory appointment;
	
	@Column(name="test_result")
	public String testResult;

    // -- Queries

    public static Model.Finder<String,Tests> find = new Model.Finder(String.class, Tests.class);

/*    public static List<Tests> getTests() {
        return find
        	.where()
            .findList();
    }
*/   
    public static int deleteTests(long idAppointment){
       	String sql = "delete from tests where appointment_id=:id";
       	SqlUpdate sqlQuery = Ebean.createSqlUpdate(sql);
       	sqlQuery.setParameter("id", idAppointment);
       	int state= sqlQuery.execute();
       	return state;
       	
       }

    public static List<Tests> getTests (long id) {
        return find
        		.fetch("examination")
        		.fetch("appointment")
        		.where()        		
        		.eq("appointment_id", id)
        		.findList();
    }

    public static List<Tests> getDetails (long id) {
        return find
        		.fetch("examination")
        		.fetch("appointment")
        		.where()        		
        		.eq("idPatient", id)
        		.findList();
    }
}

